package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.login.activitys.LoginActivity;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.main.adapters.HomeHotVerticalVideoListAdapter;
import com.xxx.video.main.adapters.VideoDetailsAdapter;
import com.xxx.video.main.beans.VideoDetailsBean;
import com.xxx.video.main.beans.VideoListBean;
import com.xxx.video.main.presenters.IVideoDetailsFragmentView;
import com.xxx.video.main.presenters.VideoDetailsFragmentPresenter;
import com.xxx.video.mine.activitys.DownloadManagementActivity;
import com.xxx.video.mine.activitys.FavoritesActivity;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class VideoDetailsFragment extends BaseFragment<VideoDetailsFragmentPresenter> implements IVideoDetailsFragmentView {

    @BindView(R.id.rcv_video_details_fragment_list)
    RecyclerView rcvVideoDetailsFragmentList;
    private ConcatAdapter mConcatAdapter;

    /**
     * video Id
     */
    private String mVideoId;
    /**
     * type 0 content  1 dynamic
     */
    private int mType;
    /**
     * adapter
     */
    private VideoDetailsAdapter mVideoDetailsAdapter;

    @Override
    protected VideoDetailsFragmentPresenter createPresenter() {
        return new VideoDetailsFragmentPresenter(Objects.requireNonNull(getActivity()), this);
    }

    public static VideoDetailsFragment newInstance(Bundle args) {
        VideoDetailsFragment fragment = new VideoDetailsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_video_details;
    }

    @Override
    public void initData(Bundle extras) {
        if (extras != null) {
            mVideoId = extras.getString("Id");
            mType = extras.getInt("Type");
        }
        initList();
    }

    @Override
    public void refreshData(Bundle extras) {
        getPresenter().getContentInfo(mVideoId);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirst) {
            isFirst = false;
            getPresenter().getContentInfo(mVideoId);
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        rcvVideoDetailsFragmentList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mConcatAdapter = new ConcatAdapter();
        rcvVideoDetailsFragmentList.setAdapter(mConcatAdapter);
    }

    @Override
    public void initVideoDetailsAdapter(VideoDetailsBean contentInfo) {
        mVideoDetailsAdapter = new VideoDetailsAdapter(Objects.requireNonNull(getActivity()));
        mConcatAdapter.addAdapter(mVideoDetailsAdapter);
        mVideoDetailsAdapter.setVideoData(contentInfo);
        mVideoDetailsAdapter.setOnItemClickListener((o, position, type) -> {
            if (type == 1) {
                getPresenter().fabulous(mVideoId);
            } else if (type == 2) {
                getPresenter().dislike(mVideoId);
            } else if (type == 3) {
                startDownloadActivity();
            } else if (type == 4) {
                getPresenter().downloadFollow(mVideoId, String.valueOf(mType));
            } else if (type == 5) {
                /*
                 * favorite
                 */
                getPresenter().favorite(contentInfo.status, mVideoId);
            }
        });
    }

    @Override
    public void initVideoDetailsRecommendListAdapter(List<VideoListBean> list) {
        HomeHotVerticalVideoListAdapter mHotVerticalVideoListAdapter = new HomeHotVerticalVideoListAdapter(Objects.requireNonNull(getActivity()));
        mConcatAdapter.addAdapter(mHotVerticalVideoListAdapter);
        mHotVerticalVideoListAdapter.setData(list);
        mHotVerticalVideoListAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<VideoListBean>) (videoListBean, position, type) -> startVideoDetailsActivity(videoListBean.fContentId));
    }

    @Override
    public void startVideoDetailsActivity(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", id);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void startDownloadActivity() {
        intent2Activity(DownloadManagementActivity.class);
    }

    @Override
    public void startLoginActivity() {
        intent2Activity(LoginActivity.class);
    }


    @Override
    public void starSuccess(boolean praise) {
        if (!praise) {
            if (mVideoDetailsAdapter != null) {
                mVideoDetailsAdapter.starPlus();
            }
            ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_star_success));
        } else {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_can_not_repeat_like));
        }
    }

    @Override
    public void unLikeSuccess(boolean praise) {
        if (!praise) {
            if (mVideoDetailsAdapter != null) {
                mVideoDetailsAdapter.unlikePlus();
            }
        } else {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_can_not_repeat_un_like));
        }
    }

    @Override
    public void addDownloadSuccess() {
        startDownloadActivity();
    }

    @Override
    public void collectionSuccess() {
        if (mVideoDetailsAdapter != null) {
            mVideoDetailsAdapter.collectionSuccess(true);
            ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_favorite_success));
        }
    }

    @Override
    public void unCollectionSuccess() {
        if (mVideoDetailsAdapter != null) {
            mVideoDetailsAdapter.collectionSuccess(false);
        }
    }

    @Override
    public void startFavoriteActivity() {
        intent2Activity(FavoritesActivity.class);
    }

    @Override
    public void initData2View(VideoDetailsBean successOb) {
        showRootLayoutStatus(1);
        if (successOb.contentInfo != null) {
            initVideoDetailsAdapter(successOb);
        }
        initVideoDetailsRecommendListAdapter(successOb.page.list);
    }
}
